package com.wyw.leetcode.learning.simple;

/**
 * leetcode topic 344
 * 反转字符串
 * 不使用额外空间
 *
 * @Author Mr Wu （yewen.wu.china@gmail.com）
 * @Date 2021/11/8 16:18
 */
public class Topic344 {

    public static void main(String[] args) {
        char[] s = new char[]{'H', 'E', 'L', 'L', 'O'};
        char[] s1 = new char[]{'N', 'A', 'M', 'E'};
        reverseString(s);
        reverseString(s1);
        System.out.println(s);
        System.out.println(s1);
    }

    public static void reverseString(char[] s) {
        int i=0;
        int j = s.length -1;
        while(i < j) {
            char temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            i++;
            j--;
        }
    }

    public static void reverseString1(char[] s) {
        int n = s.length;
        for (int i = 0; i < n / 2; ++i) {
            int j = n - 1 - i;
            s[i] ^= s[j];
            s[j] ^= s[i];
            s[i] ^= s[j];
        }
    }
}
